package net.yb.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 定义一个Servlet，继承自HttpServlet
 */
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("接收到用户的请求。。。。。。。。");

        /**
         * 获取请求参数
         */
        req.setCharacterEncoding("utf-8"); // 设置请求编码，防止乱码
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        /**
         * 响应浏览器
         */
        resp.setContentType("text/html; charset=utf-8");
        PrintWriter out = resp.getWriter();

        if("admin".equals(username) && "123".equals(password)){
            out.print("<h1 style='color:green;'>登录成功！</h1>");
        }else{
            out.print("<h1 style='color:red;'>登录失败！</h1>");
        }
        out.close();
    }
}
